<?php
/**
 * 整车管理控制器
 * Class CarloadApp
 * fhc
 */
class CarloadApp extends BackendApp
{
	var $goodsnormal_mod;
	var $goodsnormalsub_mod;
	var $_waybill_mod;
	var $_dictionary_mod;
	var $_user_mod;
	var $_haul_mod;
	var $canedit;

	function __construct()
	{
		$this->CarloadApp();
	}

	function CarloadApp()
	{
		parent::BackendApp();
		$this->_goodsnormal_mod =& m('goodsnormal');
		$this->_goodsnormalsub_mod =& m('goodsnormalsub');
		$this->_waybill_mod =& m('waybill');
		$this->_dictionary_mod =& m('dictionary');
		$this->_haul_mod =& m('haul');
		$this->_user_mod =& m('member');
		//是否可编辑
		$this->canedit=false;
	}
	//整车列表
	function index()
	{
		$db=&db();
		$status=array();
		$sql="SELECT d_value FROM zys_dictionary WHERE d_code='wstatus'";
		$wstatus=$db->getAll($sql);
		foreach ($wstatus as  $v){
			$status[$v['d_value']]=$v['d_value'];
		}
		$this->assign('status', $status);
		//条件
		$wm_carrier=empty($_GET['wm_carrier'])?'':$_GET['wm_carrier'];
		$mstatus=empty($_GET['status'])?'':$_GET['status'];
		$conditions = "1=1 and wm_type='整车'";
		if($wm_carrier){
			$conditions.=" and wm_carrier LIKE '%".$wm_carrier."%'";
		}
		if($mstatus!=null&&$mstatus!=''){
			$conditions.=" and wm_stauts='".$mstatus."' ";
		}
		$user_ids = $this->checkpower();
		if(!empty($user_ids)){
			$conditions.=" and wm_optid in(".$user_ids.")";
		}
		//数据
		$page   =   $this->_get_page(10);   //获取分页信息
		//找出所有整车的运单
		$cars=$this->_waybill_mod->find(array(
	        'fields'   => 'this.*,member.user_name', 
		    'conditions'  => $conditions,
	        'limit'   => $page['limit'],
			'order'   => 'wm_tid desc',
			'join'=>'belongs_to_user',
        	'count'   => true   //允许统计
		));

		$page['item_count']=$this->_waybill_mod->getCount();   //获取统计数据
		//赋值
		$this->assign('canedit', $this->canedit);
		$this->_format_page($page);
		$this->assign('page_info', $page);
		$this->assign('cars', $cars);
		$this->import_resource(
			array(
				'script' => 'inline_edit.js,jquery.ui/jquery.ui.js,jquery.ui/i18n/' . i18n_code() . '.js',
                'style'=> 'jquery.ui/themes/ui-lightness/jquery.ui.css'
			)
		);
		$this->assign('tabself', json_encode(array('text'=>'整车运单')));
		$this->display('carload.index.html');
	}


	//新建货单，货单明细，运单
	function add()
	{
        $db=&db();
		$car_types  =array();
		$car_types['大型车'] = '大型车';
		$car_types['小型车'] = '小型车';
		$this->assign('car_types',$car_types);
		//运单id
        $id = isset($_GET['id'])?intval($_GET['id']):0;
        $this->assign('wm_tid',$id);
		if (!IS_POST)
		{
			$db=&db();
			$sql="select * from zys_operator where o_consignor=1";
			$huo=$db->getAll($sql);
			$h=array();
			foreach ($huo as $v){
				$h[$v['o_tid']]=$v['o_name'];
			}
			$this->assign('h', $h);
			if(empty($id)){
				$waybill=array();
				$waybill['wm_type'] = '整车';
				$waybill['wm_stauts']='新建';
				$waybill['wm_code']=$this->autocode();
				$this->assign('waybill', $waybill);
				$this->canedit=true;
			}else{
				$waybill=$this->_waybill_mod->get(array(
					'fields'   => 'this.*,member.user_name',
					'conditions'  => 'wm_tid='.$id,
					'join'=>'belongs_to_user',
				));
				if($waybill['wm_stauts']=='新建'||$waybill['wm_stauts']=='已接单'||$waybill['wm_stauts']=='已派'){
					$this->canedit=true;
				}
				$this->assign('waybill', $waybill);
				//货单
				$sql_goodid = "select gs.gs_gtid from zys_waybill_sub_normal as ws left join zys_goods_normal_sub as gs on ws.ws_gstid = gs.gs_tid where ws_wmtid=".$id;
				$gtid = intval($db->getone($sql_goodid));
				$carload=$this->_goodsnormal_mod->get(array(
					'fields'   => 'this.*',
					'conditions'  => 'g_tid='.$gtid,
				));
				$this->assign('carload', $carload);
				//监控方案
				$monitor=array();
				$sql="select * from zys_monitor_main";
				$m=$db->getAll($sql);
				if($m){
					foreach ($m as $v){
						$monitor[$v['zmm_tid']]=$v['zmm_name'];
					}
				}
				$this->assign('monitor', $monitor);
				//运程方案
				$hauldefine=array();
				$sql="select * from zys_haul_define_main where zhdm_type='".$waybill['wm_type']."'";
				$h=$db->getAll($sql);
				if($h){
					foreach ($h as $v){
						$hauldefine[$v['zhdm_tid']]=$v['zhdm_name'];
					}
				}
				$this->assign('hauldefine', $hauldefine);
				//确认类型
				$sql="SELECT d_value,d_key FROM zys_dictionary WHERE d_code='haultype'";
				$typelist=array();
				$temp=$db->getAll($sql);

				foreach ($temp as $v){
					$typelist[$v['d_key']]=$v['d_value'];
				}
				$this->assign('typelist', $typelist);
				//运程采集类型
				$sql="SELECT d_value,d_key FROM zys_dictionary WHERE d_code='h_collect_type'";
				$typelist=array();
				$temp=$db->getAll($sql);

				foreach ($temp as $v){
					$typelist[$v['d_key']]=$v['d_value'];
				}
				$this->assign('ctype', $typelist);
				//运程信息
				$haullist=array();
				$sql="SELECT * from zys_haul where h_wmtid=".$id." ORDER BY h_order";
				$haul=$db->getAll($sql);
				if($haul){
					foreach ($haul as $v){
						$sql="SELECT * from zys_haul_collect WHERE hc_h_tid=".$v['h_tid'];
						$collect=$db->getAll($sql);
						if($collect){
							foreach ($collect as $vv){
								$arrs=array();
								if($vv['h_collect_val']){
									$vv['h_collect_val']=trim($vv['h_collect_val'],',');
									$arrs=explode(',', $vv['h_collect_val']);
								}
								$vv['ar']=$arrs;
								$v['item'][]=$vv;
							}
						}
						$haullist[]=$v;
					}
				}
				$this->assign('haullist', $haullist);
				//异常信息
				$exception=array();
				$sql="SELECT * FROM zys_exception WHERE e_w_tid=$id";
				$exception=$db->getAll($sql);
				if($exception){
					foreach ($exception as $k=>$v){
						$sql="SELECT * FROM zys_exception_attr WHERE ea_type=0 and ea_e_tid=".$v['e_tid'];
						$exception[$k]['e_pic']=$db->getAll($sql);

						$sql="SELECT * FROM zys_exception_attr WHERE ea_type!=0 and ea_e_tid=".$v['e_tid'];
						$exception[$k]['e_voice']=$db->getAll($sql);
					}
				}
				$this->assign('exception', $exception);
			}
			/* 导入jQuery的表单验证插件 */
			$this->import_resource(array('script' => 'inline_edit.js,jquery.ui/jquery.ui.js,jquery.ui/i18n/' . i18n_code() . '.js,jquery.plugins/jquery.validate.js',
        'style'=> 'jquery.ui/themes/ui-lightness/jquery.ui.css'));
			$this->assign('canedit', $this->canedit);
			$this->display('carload.form.html');
		}else {
			$g_owner = isset($_POST['g_owner']) && !empty($_POST['g_owner']) ? trim($_POST['g_owner']): '';
			$g_owner_tel = isset($_POST['g_owner_tel']) && !empty($_POST['g_owner_tel']) ?trim($_POST['g_owner_tel']):'';
			$g_loading_place = isset($_POST['g_loading_place']) && !empty($_POST['g_loading_place']) ?trim($_POST['g_loading_place']):'';
			$g_loading_lng = isset($_POST['g_loading_lng']) && !empty($_POST['g_loading_lng']) ?trim($_POST['g_loading_lng']):'';
			$g_loading_lat = isset($_POST['g_loading_lat']) && !empty($_POST['g_loading_lat']) ?trim($_POST['g_loading_lat']):'';
			$g_unloading_place = isset($_POST['g_unloading_place']) && !empty($_POST['g_unloading_place']) ?trim($_POST['g_unloading_place']):'';
			$g_unloading_lng = isset($_POST['g_unloading_lng']) && !empty($_POST['g_unloading_lng']) ?trim($_POST['g_unloading_lng']):'';
			$g_unloading_lat = isset($_POST['g_unloading_lat']) && !empty($_POST['g_unloading_lat']) ?trim($_POST['g_unloading_lat']):'';
			$g_consignee = isset($_POST['g_consignee']) && !empty($_POST['g_consignee']) ?trim($_POST['g_consignee']):'';
			$g_consignee_tel = isset($_POST['g_consignee_tel']) && !empty($_POST['g_consignee_tel']) ?trim($_POST['g_consignee_tel']):'';
			$g_consignor = isset($_POST['g_consignor']) && !empty($_POST['g_consignor']) ?trim($_POST['g_consignor']):'';
			$g_consignor_tel = isset($_POST['g_consignor_tel']) && !empty($_POST['g_consignor_tel']) ?trim($_POST['g_consignor_tel']):'';
			if(empty($g_owner) || empty($g_owner_tel) || empty($g_loading_place) || empty($g_unloading_place) || empty($g_consignee) || empty($g_consignee_tel) || empty($g_consignor) || empty($g_consignor_tel)){
				$this->show_warning('填写有误',	'',   'index.php?app=carload&act=add&id=' . $id);
			}
			try{
				//货单主表
				$goods_array = array(
					'g_type'     => '整车',
					'g_status'   => '新建',
					'g_code'     => 'T'.gmtime(),
					'g_ownertid' => intval($_POST['g_ownertid']),
					'g_owner'    => $g_owner,
					'g_owner_tel'     => $g_owner_tel,
					'g_loading_place' => $g_loading_place,
					'g_loading_lng' => $g_loading_lng,
					'g_loading_lat' => $g_loading_lat,
					'g_unloading_place'    => $g_unloading_place,
					'g_unloading_lng' => $g_unloading_lng,
					'g_unloading_lat' => $g_unloading_lat,
					'g_consignee'     => $g_consignee,
					'g_consignee_tel' => $g_consignee_tel,
					'g_consignor'    => $g_consignor,
					'g_consignor_tel'    => $g_consignor_tel,
					'g_note'			=> trim($_POST['g_note']),
					'g_optid'      => $this->visitor->get('user_id'),
					'g_opdate'     => gmtime(),
					//'g_tgdate'     => gmstr2time($_POST['g_tgdate']),
					//'g_rdate'     => gmstr2time($_POST['g_rdate']),
					'g_loading_date'     => gmstr2time($_POST['g_loading_date']),
					'g_unloading_date'     => gmstr2time($_POST['g_unloading_date']),
					'g_cartype'    => trim($_POST['g_cartype'])
				);
				$userid = $this->visitor->get('user_id');
				$unit = $this->visitor->get('unit');
				$tag ='';
				if($id){
					$tag=1;
					unset($goods_array['g_type']);
					unset($goods_array['g_status']);
					unset($goods_array['g_code']);
					//获取货单id
					$sql_goodid = "select gs.gs_gtid from zys_waybill_sub_normal as ws left join zys_goods_normal_sub as gs on ws.ws_gstid = gs.gs_tid where ws_wmtid=".$id;
					$gtid = intval($db->getone($sql_goodid));
					$num = $this->checkaddr($g_loading_place,$userid,$unit);
					if($num==0){
						$loading = array(
							'user_id'    	=>	$userid,
							'consignee' 	=>	$g_consignor,
							'address' 		=>	$g_loading_place,
							'phone_tel'	=>	$g_consignor_tel,
							'lng'			=>	$g_loading_lng,
							'lat'			=>	$g_loading_lat,
							'addrtype'		=>	'整车装货',
							'unit'			=>	$unit,
						);
						$this->addaddr($loading);
					}
					$num1 = $this->checkaddr($g_unloading_place,$userid,$unit);
					if($num1==0){
						$unloading = array(
							'user_id'    	=>	$userid,
							'consignee' 	=>	$g_consignee,
							'address' 		=>	$g_unloading_place,
							'phone_tel'	=>	$g_consignee_tel,
							'lng'			=>	$g_unloading_lng,
							'lat'			=>	$g_unloading_lat,
							'addrtype'		=>	'整车卸货',
							'unit'			=>	$unit,
						);
						$this->addaddr($unloading);
					}
					$this->_goodsnormal_mod->edit($gtid,$goods_array);
					$this->show_message('编辑成功',	'返回编辑',   'index.php?app=carload&act=add&id=' . $id);
				}else{
					$tag=2;
					$g_tid = $this->_goodsnormal_mod->add($goods_array);
					if(empty($g_tid)){
						throw new Exception('操作失败');
					}
					//货单子表
					$gongds_sub_array = array(
						'gs_gtid' =>$g_tid,
						'gs_name' =>'整车货物'
					);
					$gs_tid = $this->_goodsnormalsub_mod->add($gongds_sub_array);
					if(empty($gs_tid)){
						throw new Exception('操作失败');
					}
					//运单主表
					$waybill_array = array(
						'wm_stauts' => '新建',
						'wm_type'   => '整车',
						'wm_code'   => $this->autocode(),
						'wm_optid'	 =>$this->visitor->get('user_id'),
						'wm_opdate'=>gmtime()
					);
					$wm_tid = $this->_waybill_mod->add($waybill_array);
					if(empty($wm_tid)){
						throw new Exception('操作失败');
					}
					//运单子表
					$waybill_sub_array = array(
						'ws_gid'=>$g_tid,
						'ws_gstid'=>$gs_tid,
						'ws_wmtid'=>$wm_tid,
					);
					$sub_normal = & m('waybillnormal');
					$ws_tid = $sub_normal->add($waybill_sub_array);
					if(empty($ws_tid)){
						throw new Exception('操作失败');
					}
					$num = $this->checkaddr($g_loading_place,$userid,$unit);
					if($num==0){
						$loading = array(
							'user_id'    	=>	$userid,
							'consignee' 	=>	$g_consignor,
							'address' 		=>	$g_loading_place,
							'phone_tel'	=>	$g_consignor_tel,
							'lng'			=>	$g_loading_lng,
							'lat'			=>	$g_loading_lat,
							'addrtype'		=>	'整车装货',
							'unit'			=>	$unit,
						);
						$this->addaddr($loading);
					}
					$num1 = $this->checkaddr($g_unloading_place,$userid,$unit);
					if($num1==0){
						$unloading = array(
							'user_id'    	=>	$userid,
							'consignee' 	=>	$g_consignee,
							'address' 		=>	$g_unloading_place,
							'phone_tel'	=>	$g_consignee_tel,
							'lng'			=>	$g_unloading_lng,
							'lat'			=>	$g_unloading_lat,
							'addrtype'		=>	'整车卸货',
							'unit'			=>	$unit,
						);
						$this->addaddr($unloading);
					}
				}
				if ($tag==1){

					$this->show_message('编辑成功',	'返回编辑',   'index.php?app=carload&act=add&id=' . $wm_tid);

				}elseif ($tag==2){
					$this->show_message('添加成功',	'返回编辑',   'index.php?app=carload&act=add&id=' . $wm_tid);
				}
			}catch (Exception $e){
				$this->show_message($e->getMessage(),	'返回添加',   'index.php?app=carload&act=add');
			}
		}
	}
	//监控方案
	function monitor(){
		$db=&db();
		$id = empty($_POST['id']) ? 0 : intval($_POST['id']);
		$mid = empty($_POST['mid']) ? 0 : intval($_POST['mid']);
		$waybill=$this->_waybill_mod->get(array(
			'fields'   => 'this.*',
			'conditions'  => 'wm_tid='.$id,
		));

		if($waybill['wm_stauts']!='新建'&&$waybill['wm_stauts']!='已接单'){
			$this->show_warning('运单不能修改','',   'index.php?app=waybill&act=edit&id='.$id);
			return;
		}

		$data = array('wm_zmmtid'  => $mid);

		$wid = $this->_waybill_mod->edit($id,$data);
		//返回数据
		$this->json_result($data,'选择监控方案成功');
	}
	//运程方案
	function hauldefine(){
		$db=&db();
		$id = empty($_GET['id']) ? 0 : intval($_GET['id']);
		$hid = empty($_GET['hid']) ? 0 : intval($_GET['hid']);

		$waybill=$this->_waybill_mod->get(array(
			'fields'   => 'this.*',
			'conditions'  => 'wm_tid='.$id,
		));

		if($waybill['wm_stauts']!='新建'&&$waybill['wm_stauts']!='已接单'&&$waybill['wm_stauts']!='已派'){
			$this->show_warning('运单不能修改','',   'index.php?app=carload&act=add&id='.$id);
			return;
		}

		$this->addhauldeploy($hid,$id);

		$this->show_message('选择进程方案成功',
			'返回运单',   'index.php?app=carload&act=add&id='.$id
		);
	}
	//引入运程方案
	function addhauldeploy($hid,$h_wmtid)
	{
		$db=&db();

		$sql="delete from zys_haul_collect where exists(select 1 from zys_haul where hc_h_tid=h_tid and h_wmtid=".$h_wmtid.")";
		$db->query($sql);

		$sql="delete from zys_haul where h_wmtid=".$h_wmtid;
		$db->query($sql);

		$sql="SELECT * FROM zys_haul_define WHERE h_zhdmtid='".$hid."'";
		$temp=$db->getAll($sql);

		if($temp){
			foreach ($temp as $v){
				if($v['h_address']=='@C_load' || $v['h_address']=='@C_unload' || $v['h_linkman']=='@C_fhr' || $v['h_linkman']=='@C_shr' || $v['h_linkman_tel']=='@C_linktel' || $v['h_linkman_tel']=='@C_fhr_tel' || $v['h_linkman_tel']=='@C_shr_tel'
					|| $v['h_date']=='@C_load_time' || $v['h_date']=='@C_unload_time' 	){
					$sql = "select gn.g_loading_date,gn.g_unloading_date,gn.g_owner_tel,gn.g_loading_place,gn.g_loading_lng,gn.g_loading_lat,gn.g_unloading_place,gn.g_unloading_lng,gn.g_unloading_lat,gn.g_consignee,gn.g_consignee_tel,gn.g_consignor,gn.g_consignor_tel from zys_goods_normal as gn left join zys_waybill_sub_normal as wsn on gn.g_tid= wsn.ws_gid where wsn.ws_wmtid=".$h_wmtid." GROUP BY ws_gid";
					$row = $db->getrow($sql);
					switch($v['h_linkman']){
						case '@C_fhr':
							$data['h_linkman'] = $row['g_consignor'];
							break;
						case '@C_shr':
							$data['h_linkman'] = $row['g_consignee'];
							break;
						default:
							$data['h_linkman'] = $v['h_linkman'];
							break;
					}
					switch($v['h_linkman_tel']){
						case '@C_linktel':
							$data['h_linkman_tel'] = $row['g_owner_tel'];
							break;
						case '@C_fhr_tel':
							$data['h_linkman_tel'] = $row['g_consignor_tel'];
							break;
						case '@C_shr_tel':
							$data['h_linkman_tel'] = $row['g_consignee_tel'];
							break;
						default:
							$data['h_linkman_tel'] = $v['h_linkman_tel'];
							break;
					}
					switch($v['h_address']){
						case '@C_load':
							$data['h_address'] = $row['g_loading_place'];
							$data['h_lng'] = $row['g_loading_lng'];
							$data['h_lat'] = $row['g_loading_lat'];
							break;
						case '@C_unload':
							$data['h_address'] = $row['g_unloading_place'];
							$data['h_lng'] = $row['g_unloading_lng'];
							$data['h_lat'] = $row['g_unloading_lat'];
							break;
						default:
							$data['h_address'] = $v['h_address'];
							break;
					}
					switch($v['h_date']){
						case '@C_load_time':
							$data['h_required_service_date'] = $row['g_loading_date'];
							break;
						case '@C_unload_time':
							$data['h_required_service_date'] = $row['g_unloading_date'];
							break;
						default:
							$data['h_required_service_date'] = $v['h_date'];
							break;
					}

				}else{
					$data['h_address'] = $v['h_address'];
					$data['h_linkman'] = $v['h_linkman'];
					$data['h_linkman_tel'] = $v['h_linkman_tel'];
					$data['h_required_service_date'] = $v['h_date'];
				}

				$data['h_wmtid'] = $h_wmtid;
				$data['h_name'] = $v['h_name'];
				$data['h_short_name'] = $v['h_short_name'];

				$data['h_order'] = $v['h_order'];
				$data['h_type'] = $v['h_type'];
				$data['h_range'] = intval($v['h_range']);

				$id = $this->_haul_mod->add($data);

				$sql="select * from zys_haul_define_collect where hc_h_tid=".$v['h_tid'];
				$collect=$db->getAll($sql);
				if($collect){
					foreach ($collect as $c){
						$sql="insert into zys_haul_collect set hc_h_tid='".$id."',
						h_collect_type='".$c['h_collect_type']."',h_collect_des='".$c['h_collect_des']."',h_collect_val='".$c['h_collect_val']."'";
						$db->query($sql);
					}
				}
			}
		}
	}

	//派车保存
	function pcsave() {
		$wid=empty($_POST['wm_tid'])?0:intval($_POST['wm_tid']);
		$wm_driver=empty($_POST['wm_driver'])?'':$_POST['wm_driver'];
		$oper=empty($_POST['operator'])?0:intval($_POST['operator']);
		$wm_vhc_code=empty($_POST['wm_vhc_code'])?'':$_POST['wm_vhc_code'];
		$wm_driver_tel=empty($_POST['wm_driver_tel'])?'':$_POST['wm_driver_tel'];
		$wm_requiredl_note=empty($_POST['wm_requiredl_note'])?'':$_POST['wm_requiredl_note'];
		$wm_requiredl_note_voice=empty($_POST['wm_requiredl_note_voice'])?'':$_POST['wm_requiredl_note_voice'];
		$db=&db();
		$sql="SELECT o_name from zys_operator WHERE o_tid=".$oper;
		$oname=$db->getOne($sql);
		$sql="update zys_waybill_main set wm_vhc_code='".$wm_vhc_code."',wm_driver='".$wm_driver."'
			,wm_driver_tel='".$wm_driver_tel."',wm_carrier='".$oname."'
			,wm_carrier_id=$oper,wm_requiredl_note='".$wm_requiredl_note."',wm_requiredl_note_voice='".$wm_requiredl_note_voice."'
			 where wm_tid=".$wid;
		$db->query($sql);
		$this->show_message('edit_ok',	'返回运单',   'index.php?app=carload&act=add&id=' . $wid
		);
	}
	/**
	 * 发车处理
	 */
	function paichedone() {
		$db=&db();
		$wid=empty($_GET['wm_tid'])?0:intval($_GET['wm_tid']);
		$uid=empty($_GET['wm_user'])?0:intval($_GET['wm_user']);

		$sql="select * from zys_waybill_main where wm_tid=".$wid;
		$w=$db->getRow($sql);

		$sql="select * from zys_member where user_id=".$uid;
		$user=$db->getRow($sql);
		if(!empty($wid)){
			//修改运单状态
			$sql="UPDATE zys_waybill_main SET wm_stauts='已派' WHERE wm_tid=$wid";
			$db->query($sql);

			//修改运单信息
			$sql="UPDATE zys_waybill_main SET wm_uuid='".$user['uuid']."' WHERE wm_tid=$wid";
			$db->query($sql);


			//发送推送消息
			require_once ROOT_PATH.'/baidupush/sdk.php';
			$sdk = new PushSDK();
			$channelId = $user['channelId']?$user['channelId']:'3849309310203392735';
			try{

				$opts = array (
					'msg_type' => 0
				);
				if($w['wm_type']=='集卡进口 ') {
					$sql="select * from zys_goods_trunk where exists(select 1 from zys_waybill_sub_trunk where g_tid=ws_gid and ws_wmtid=".$wid.")";
					$gt=$db->getRow($sql);

					$rs = $sdk->pushMsgToSingleDevice($channelId,
						'{"type":"3","result":{"wm_tid":"' . $wid . '","type":"' . $w['wm_type'] . '","note":"' . $w['wm_requiredl_note'] . ' ' . local_date('Y年m月d日 H点i分', $gt['g_collectiontime']) . '到' . $gt['g_full_container_depot'] . '背重箱
				' . local_date('Y年m月d日 H点i分', $gt['g_packingtime']) . '送至' . $gt['g_loading_place'] . '","voice":"' . $w['wm_requiredl_note_voice'] . '","intral":"30"}}', $opts);
				}
				else if($w['wm_type']=='集卡出口 ') {
					$sql="select * from zys_goods_trunk where exists(select 1 from zys_waybill_sub_trunk where g_tid=ws_gid and ws_wmtid=".$wid.")";
					$gt=$db->getRow($sql);

					$rs = $sdk->pushMsgToSingleDevice($channelId,
						'{"type":"3","result":{"wm_tid":"' . $wid . '","type":"' . $w['wm_type'] . '","note":"' . $w['wm_requiredl_note'] . ' ' . local_date('Y年m月d日 H点i分', $gt['g_packingtime']) . '到' . $gt['g_loading_place'] . '装货
				' . local_date('Y年m月d日 H点i分', $gt['g_collectiontime']) . '前送至' . $gt['g_full_container_depot'] . '","voice":"' . $w['wm_requiredl_note_voice'] . '","intral":"30"}}', $opts);
				}
				else {
					$rs = $sdk->pushMsgToSingleDevice($channelId,
						'{"type":"3","result":{"wm_tid":"' . $wid . '","type":"' . $w['wm_type'] . '","note":"' . $w['wm_requiredl_note'] . ' ' . local_date('Y年m月d日 H点i分', $w['wm_loading_date']) . '到' . $w['wm_loading_place'] . '提货
				' . local_date('Y年m月d日 H点i分', $w['wm_required_service_date']) . '送至' . $w['wm_unloading_place'] . '","voice":"' . $w['wm_requiredl_note_voice'] . '","intral":"30"}}', $opts);
				}
			}catch (Exception $ex){

			}
		}
		$this->show_message('派车成功',	'返回运单','index.php?app=carload&act=add&id=' . $wid
		);

	}

	//运程信息处理
	function hauledit(){
		$db=&db();
		$id=empty($_POST['wm_tid'])?0:intval($_POST['wm_tid']);
		$waybill=$this->_waybill_mod->get(array(
			'fields'   => 'this.*',
			'conditions'  => 'wm_tid='.$id,
		));

		if($waybill['wm_stauts']!='新建'&&$waybill['wm_stauts']!='已接单'&&$waybill['wm_stauts']!='已派'){
			$this->show_warning('运单不能修改','返回运单',   'index.php?app=carload&act=add&id='.$id);
			return;
		}

		$haul=empty($_POST['haul'])?array():$_POST['haul'];

		if($haul){
			$haul=array_values($haul);
			foreach ($haul as $k=>$v){
				$h_tid = 0;
				if($v['h_tid']){
					$h_tid = $v['h_tid'];
					$sql="update zys_haul set h_name='".$v['h_name']."',h_linkman='".$v['h_linkman']."',
					h_linkman_tel='".$v['h_linkman_tel']."',h_type='".$v['h_type']."',h_range='".$v['h_range']."',h_required_service_date='".gmstr2time($v['h_required_service_date'])."',
					h_note='".$v['h_note']."',h_address='".$v['h_address']."',h_lat='".$v['h_lat']."',h_lng='".$v['h_lng']."' where h_tid=".$v['h_tid'];

					$db->query($sql);
				}else{
					$data['h_wmtid'] = $id;
					$data['h_name'] = $v['h_name'];
					$data['h_short_name'] = $v['h_name'];
					$data['h_linkman'] = $v['h_linkman'];
					$data['h_linkman_tel'] = $v['h_linkman_tel'];
					$data['h_address'] = $v['h_address'];
					$data['h_order'] = 255;
					$data['h_range'] = intval($v['h_range']);
					$data['h_type'] = $v['h_type'];
					$data['h_required_service_date'] = gmstr2time($v['h_required_service_date']);
					$data['h_note'] = $v['h_note'];
					$data['h_lat'] = $v['h_lat'];
					$data['h_lng'] = $v['h_lng'];
					$h_tid = $this->_haul_mod->add($data);
				}
				if($v['item']){
					$v['item']=array_values($v['item']);
					foreach ($v['item'] as $kk=>$vv){
						if($vv['hc_tid']){
							$sql="update zys_haul_collect set h_collect_type='".$vv['h_collect_type']."',h_collect_des='".$vv['h_collect_des']."' where hc_tid=".$vv['hc_tid'];
							$db->query($sql);
						}else{
							$sql="insert into zys_haul_collect set hc_h_tid='".$h_tid."',
						h_collect_type='".$vv['h_collect_type']."',h_collect_des='".$vv['h_collect_des']."'";
							$db->query($sql);
						}
					}
				}

			}
		}

		$this->show_message('编辑运程成功',
			'返回运单',   'index.php?app=carload&act=add&id='.$id
		);
	}
	/**
	 * 异常处理
	 */
	function exceptiondone() {
		$db=&db();
		$wid=empty($_POST['wm_tid'])?0:intval($_POST['wm_tid']);
		$exp=empty($_POST['exp'])?array():$_POST['exp'];
		if($exp){
			$exp=array_values($exp);
			foreach ($exp as $k=>$v){
				if($v['e_tid']){
					$sql="UPDATE zys_exception SET e_staus='已处理',e_handle_person='".$v['e_handle_person']."'	,e_handle_date='".gmstr2time($v['e_handle_date'])."',e_handle_note='".$v['e_handle_note']."' WHERE e_tid=".$v['e_tid']."";
					$db->query($sql);
				}
			}
		}
		$this->show_message('处理成功',	'返回运单',   'index.php?app=carload&act=add&id=' . $wid);
	}

	//删除
	function drop()
	{
		$arr = isset($_GET['id']) ? trim($_GET['id']) : '';
		$db=&db();
		$arrs=explode(',', $arr);
		$ids = implode(',',array_filter($arrs));
		if($arrs!=null&&count($arrs)>0){
			try{
				$sql = "select ws_tid,ws_gstid,ws_new from zys_waybill_sub_normal where ws_wmtid in(".$ids.")";
				$ws = $db->getAll($sql);
				foreach($ws as $val){
					$ws_new = $val['ws_new'];
					$ws_tid = $val['ws_tid'];
					if($ws_new ==1){
						$sql_ws = "delete from zys_waybill_sub_normal where ws_tid=".$ws_tid;
						$db->query($sql_ws);
					}else{
						//找到货单子表，主表删除
						$ws_gstid = $val['ws_gstid'];
						$sql_gs = "select gs_gtid from zys_goods_normal_sub where gs_tid =".$ws_gstid;
						$gs_gtid = $db->getone($sql_gs);
						//删除货单主表
						$sql_g = "delete from zys_goods_normal where g_tid =".$gs_gtid;
						$db->query($sql_g);
						//删除货单子表
						$db->query("delete from zys_goods_normal_sub where gs_tid =".$ws_gstid);
						//删除运单子表
						$db->query("delete from zys_waybill_sub_normal where ws_tid =".$ws_tid);
					}
				}
				//删除主运单
				$sql_zys_waybill_main = "delete from zys_waybill_main where wm_tid in(".$ids.")";
				$db->query($sql_zys_waybill_main);
				$this->show_message('',	'返回列表', 'index.php?app=carload&act=index');
			}catch (Exception $e){
				$this->json_error($e->getMessage());
			}
		}else{
			$this->show_message('请选择删除项');
		}
	}
	//加入到地址管理系统中

	function addaddr($data){
		$addr = & m('addr');
		$addr->add($data);
	}
	//验证地址是否已经存在
	function checkaddr($addr,$userid,$unit){
		$db = &db();
		$sql="select count(*) as num from zys_addr where address='".$addr."' and user_id=".$userid." and unit=".$unit;
		$num = $db->getone($sql);
		return $num;
	}

}